package com.mediastory.spsserver.info.service.impl;

import java.io.File;
import java.net.URLEncoder;
import java.util.Calendar;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;

import com.mediastory.spsserver.info.dao.InfoDao;
import com.mediastory.spsserver.info.domain.Info;
import com.mediastory.spsserver.info.service.InfoSvc;
import com.Ostermiller.util.Base64;

/**
 * @author Jaewon Choi
 *
 */

@Service
@Transactional
public class InfoSvcImpl implements InfoSvc {

	@Autowired
	InfoDao infoDao;
	
	@Autowired
	private MessageSourceAccessor messages;
	
	public InfoSvcImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<Info> findbyList(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		return (List<Info>) (infoDao.SelectList(condition));
	}

	public Map<String, Object> findbyListPaging(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("infoList", (List<Info>) (infoDao.SelectList(condition)));
		resultMap.put("infoListTotalCnt", (Integer) (infoDao.SelectListTotalCnt(condition)));
		
		return resultMap;
	}

	@Override
	public Info findby(Integer idx) throws Exception {
		// TODO Auto-generated method stub
		return infoDao.select(idx);
	}

	@Override
	public Info add(Info info) throws Exception {
		// TODO Auto-generated method stub
		
		Info result = null;
		File file = null;
		try {
			//파일 업로드
			MultipartFile imgFile = info.getImgFile();
			if(imgFile != null && ! imgFile.isEmpty() && imgFile.getContentType().contains("image")){
				long time = Calendar.getInstance().getTimeInMillis();
				String prefixNm = Base64.encode((URLEncoder.encode(info.getTitle(),"utf-8")+Long.toString(time)));
				//업로드 경로
				String uploadDir = messages.getMessage("file.info.uploadDir");
				//viewUrl
				String viewUrl = messages.getMessage("file.info.viewUrl");
				
				String fileNm = prefixNm + "."+(imgFile.getOriginalFilename()).split("\\.")[1];

				file = new File(uploadDir+fileNm);
				String url = viewUrl+fileNm;
				info.setImgUrl(url);
				
				if(! file.isDirectory()){	//해당 폴더가 없을 경우 폴더 생성
					new File(uploadDir).mkdirs();
				}
				imgFile.transferTo(file);
			}
			
			//기존 데이터 삭제
			Map<String, Object> condition = new HashMap<String, Object>();
			condition.put("gubun", info.getGubun());
			infoDao.delete(condition);
			
			//신규 등록
			result = infoDao.insert(info);	
		} catch (Exception e) {
			// TODO: handle exception
			
			//업로드 파일 삭제
			if(file != null){
				file.delete();
			}
			
			throw e;
		}

		return result;
	}

	@Override
	public Info modify(Info info) throws Exception {
		// TODO Auto-generated method stub
		return infoDao.update(info);
	}

	@Override
	public int remove(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		return infoDao.delete(condition);
	}

}